/**
 * Created by walkman on 2017/7/21.
 */
/**
 * @angular 管理员
 * @description
 */

orderList = function ($scope, $ducTools, $compile, $get, http, $dataTableSet) {
    'use strict';

    var orderTable = $('#orderTable').DataTable({
        sAjaxSource: 'order/get_customer_orders',
        fnServerData: $ducTools.retrieveData,
        fnServerParams: function (aoData) {
            aoData.push(
                {"name": "userCode", "value": $scope.userCode},
                {"name": "userPhone", "value": $scope.userPhone},
                {"name": "userName", "value": $scope.userName},
                {"name": "receivePhone", "value": $scope.receivePhone},
                {"name": "receiveName", "value": $scope.receiveName},
                {"name": "orderStatus", "value": $scope.orderStatus},
                {"name": "orderNo", "value": $scope.orderNo},
                {"name": "sendNo", "value": $scope.sendNo},
                {"name": "orderStartDateTime", "value": $("#orderStartDateTime").val()},
                {"name": "orderEndDateTime", "value": $("#orderEndDateTime").val()}
            );
        },
        columns: [
            {"data": "orderNo"},
            {"data": "orderDatetime"},
            {"data": "orderSum"},
            {"data": "userCode"},
            {"data": "userName"},
            {"data": "userPhone"},
            {"data": "receiveName"},
            {"data": "receivePhone"},
            {"data": "receiveAddress"},
            {"data": "orderStatus"},
            {"data": "sendDatetime"},
            {"data": "sendNo"},
            {"data": "signDatetime"},
            {"data": null},
        ],
        columnDefs: [
            {
                targets: 9,
                render: function (orderStatus) {
                    if (orderStatus == 1) {
                        return "待发货";
                    } else if (orderStatus == 2) {
                        return "待签收";
                    } else if (orderStatus == 3) {
                        return "已签收";
                    } else if (orderStatus == 4) {
                        return "已退单";
                    }
                }
            },
            {
                targets: 13,
                render: function (c) {
                    var btn = "";
                    if (c.orderStatus == 1) {
                        btn = "<button class='btn btn-success btn-xs' ng-click=showSendNo('" + c.orderId + "')> 发货 </button> &nbsp;" +
                            "<button class='btn btn-success btn-xs' ng-click=cancelOrder('" + c.orderId + "')> 退单 </button> &nbsp;";
                    } else if (c.orderStatus == 2) {
                        btn = "<button class='btn btn-success btn-xs' ng-click=signOrder('" + c.orderId + "')> 签收 </button> &nbsp;";
                    }
                    return btn;
                }
            }
        ],
        createdRow: function (row, data, dataIndex) {
            $compile(row)($scope);
        }
    });

    $dataTableSet.dataTableRowShow('orderTable');

    $scope.orderListDraw = function () {
        orderTable.draw();
    };

    //订单号填写
    $scope.showSendNo = function (orderId) {

        $("#sendNo").modal('show');

        $scope.orderId = orderId;
    };

    $scope.cancelOrder = function (orderId) {
        http.post({"orderId": orderId}, "order/cancelOrder").then(function (data) {
            swal("成功！");
            orderTable.draw();

        });
    };

    // 发送
    $scope.signOrder = function (orderId) {

        http.post({
            "orderId": orderId
        }, "order/signOrder").then(function (data) {
            swal("操作成功");
            orderTable.draw();
        });
    };

    // 发送
    $scope.sendOrder = function () {

        http.post({
            "orderId": $scope.orderId,
            "sendNo": $scope.orderSendNo
        }, "order/sendOrder").then(function (data) {

            $("#sendNo").modal('hide');
            // swal("操作成功");
            window.location.reload(true);
        });
    };

};

